﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using weikebuluo.DAL;
using weikebuluo.Model;
using System.Data;

namespace weikeweixin.web.admin.website
{
    public partial class newsclassadd : weikeweixin.web.admin.cs.adminbase
    {
        t_newscate_dal NewsCategoryDAL = new t_newscate_dal();
        t_newscate_model NewsCategoryModel = new t_newscate_model();

        protected string PicUrl = "/web/admin/website/styles/images/home-default21.jpg";
        protected string Status = "";
        protected string Name = "";
        protected string KeyWords = "";
        protected string Description = "";
        protected string PageTitle = "添加图文分类";
        protected string HomeIcon_Id = "";
        protected string CBgColor = "";
        protected string CBgImg = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack && Request.QueryString["id"] != null)
            {
                if (string.IsNullOrEmpty(user_id))
                {
                    Response.Write("<script Language=\"Javascript\">alert(\"请先配置微官网\")");
                    Response.Write("</script>");
                    Response.End();
                }
                
                DataTable dtNC = NewsCategoryDAL.GetList("CId=" + Request.QueryString["id"]).Tables[0];

                PageTitle = " 修改图文分类";

                if (dtNC.Rows.Count > 0)
                {
                    PicUrl = dtNC.Rows[0]["PicUrl"].ToString();
                    Status = dtNC.Rows[0]["CStatus"].ToString();
                    Name = dtNC.Rows[0]["CName"].ToString();
                    KeyWords = dtNC.Rows[0]["KeyWords"].ToString();
                    Description = dtNC.Rows[0]["Description"].ToString();
                    HomeIcon_Id = dtNC.Rows[0]["HomeIcon_Id"].ToString();
                    CBgColor = dtNC.Rows[0]["CBgColor"].ToString();
                    CBgImg = dtNC.Rows[0]["CBgImg"].ToString();
                }
            }

            switch (Request.Form["action"])
            {
                case "save":
                    Save();
                    break;
            }
        }

        private void Save()
        {
            NewsCategoryModel.CName = Request.Form["classname"];
            NewsCategoryModel.CStatus = int.Parse(string.IsNullOrEmpty(Request.Form["is_home"]) ? "1" : Request.Form["is_home"]);
            NewsCategoryModel.Description = Request.Form["description"];
            NewsCategoryModel.KeyWords = Request.Form["KeyWords"];
            NewsCategoryModel.user_id = int.Parse(user_id);
            NewsCategoryModel.PicUrl = Request.Form["icon"];
            NewsCategoryModel.HomeIcon_Id = Request.Form["category_id"];
            NewsCategoryModel.CBgColor = Request.Form["CBgColor"];
            NewsCategoryModel.CBgImg = Request.Form["CBgImg"];

            if (string.IsNullOrEmpty(Request.Form["id"]))
            {
                int re = NewsCategoryDAL.Add(NewsCategoryModel);

                if (re > 0)
                {
                    Response.Write("{\"errno\":\"0\",\"tip\":\"保存成功！\",\"url\":\"newsclass.aspx\",\"error\":\"\"}");
                }
                else
                {
                    Response.Write("{\"errno\":\"0\",\"tip\":\"保存失败！\",\"url\":\"newsclass.aspx\",\"error\":\"\"}");
                }
            }
            else
            {
                NewsCategoryModel.CId = int.Parse(Request.Form["id"]);

                bool re = NewsCategoryDAL.Update(NewsCategoryModel);
                if (re)
                {
                    Response.Write("{\"errno\":\"0\",\"tip\":\"保存成功！\",\"url\":\"newsclass.aspx\",\"error\":\"\"}");
                }
                else
                {
                    Response.Write("{\"errno\":\"0\",\"tip\":\"保存失败！\",\"url\":\"newsclass.aspx\",\"error\":\"\"}");
                }
            }
            Response.End();
        }
    }
}